<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <div class="title">微人事</div>
                <div>
                    <el-button type="text" icon="el-icon-bell" size="normal"
                               style="color: black;margin-right: 8px"
                               @click="goChat"></el-button>
                    <el-dropdown class="userInfo" @command="commandHandler">
                    <span class="el-dropdown-link" style="display: flex;align-items: center" v-if="user.userFace">
                        {{user.name}}<i><img :src="user.userFace"></i>
                    </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
                            <el-dropdown-item command="setting">设置</el-dropdown-item>
                            <el-dropdown-item command="logout">注销</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </el-header>
            <el-container>
                <el-aside :width="isOpen === true?'65px':'200px'">
                    <div class="toggle-btn" @click="toggleMenu">|||</div>
                    <el-menu router
                             :collapse="isOpen"
                             background-color="rgba(255,255,255)"
                             text-color="rgba(0,0,0)"
                             :collapse-transition="false">
                        <el-submenu :index="index + ''"
                                    v-for="(item, index) in routes"
                                    :key="index"
                                    v-if="!item.hidden">
                            <template slot="title">
                                <i :class="item.iconCls" style="color: #1e90ff;margin-right: 5px"></i>
                                <span>{{item.name}}</span>
                            </template>
                            <el-menu-item :index="children.path" v-for="(children, index) in item.children">
                                {{children.name}}
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb v-if="this.$router.currentRoute.path !== '/home'"
                                   separator-class="el-icon-arrow-right">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
                    </el-breadcrumb>
                    <div class="homeWelcome" v-if="this.$router.currentRoute.path === '/home'">
                        <el-row :gutter="15">
                            <!--左边部分-->
                            <el-col :span="13">
                                <!-- 用户信息表格 -->
                                <el-card class="box-card">
                                    <div slot="header" class="clearfix">
                                        <span>用户信息</span>
                                        <el-button style="float: right;" size="mini" type="primary">用户中心</el-button>
                                    </div>
                                    <el-tooltip
                                            :v-if="user.userFace"
                                            class="item" effect="dark"
                                            content="换头像功能还未实现"
                                            placement="top-start">
                                        <el-avatar
                                                shape="square"
                                                :size="90"
                                                :src="user.userFace"
                                                style="float:left;"
                                                :key="1"
                                        ></el-avatar>
                                    </el-tooltip>
                                    <div class="right" style="float:right;width:520px;">
                                        <div>
                                            用户角色：
                                            <el-tag style="margin-right: 4px;" type="success" v-for="(role) in allRoles">{{role.nameZh}}
                                            </el-tag>
                                        </div>
                                    </div>
                                </el-card>
                            </el-col>
                            <!-- 右边部分 -->
                            <el-col :span="11">
                                <div class="grid-content bg-purple">
                                    <el-card style="min-height:700px;">
                                        <el-calendar :v-model="new Date()" style="height: 611px"></el-calendar>
                                    </el-card>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    export default {
        // inject: ['reload'],
        name: "Home",
        data() {
            return {
                isOpen: false,
                adminInfo: [],
                allRoles: [],
                // user: JSON.parse(window.sessionStorage.getItem('user'))
            }
        },
        // mounted() {
        //     this.adminInfo = JSON.parse(window.sessionStorage.getItem('user'));
        //     this.allRoles = this.adminInfo.roles;
        // },
        computed: {
            routes() {
                return this.$store.state.routes;
            },
            user() {
                return this.$store.state.currentAdmin;
            },

        },
        methods: {
            commandHandler(command) {
                if (command === 'logout') {
                    this.$confirm('此操作将注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.postRequest('/logout');
                        window.sessionStorage.removeItem('tokenStr');
                        window.sessionStorage.removeItem('user');
                        // 清空菜单
                        this.$store.commit('initRoutes', []);
                        this.$router.replace('/');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消操作'
                        });
                    });
                }
                if (command === 'userinfo') {
                    this.$router.push('/userinfo');
                }
            },
            goChat() {
                this.$router.push('/chat');
            },
            // 侧边栏伸缩
            toggleMenu() {
                this.isOpen = !this.isOpen;
                // console.log(this.$store.state.currentAdmin)
            },
            // initAllRoles() {
            //     this.getRequest('/admin/roles').then(res => {
            //         if (res) {
            //             this.allRoles = res;
            //         }
            //     })
            // },
        },
    }
</script>

<style scoped>
    .homeHeader {
        background: #aaf7ff;
        display: flex;
        align-items: center;
        /*去掉周围空袭*/
        justify-content: space-between;
        padding: 0 15px;
        box-sizing: border-box;
    }

    .homeHeader .title {
        font-size: 25px;
        font-family: 华文行楷;
        color: gray;
    }

    .homeHeader .userInfo {
        cursor: pointer;
    }

    .el-dropdown-link img {
        width: 48px;
        height: 48px;
        border-radius: 24px;
        margin-left: 8px;
    }

    .homeWelcome {
        text-align: center;
        font-size: 30px;
        color: darkturquoise;
    }

    .homeRouterView {
        margin-top: 10px;
    }

    .toggle-btn {
        background-color: #bad4f0 !important;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>